import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { addreply } from "@/api/api";


Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        titles: [], // 存放首页标题
        id: [], // 存放 首页分类id
        author: '', // 存放用户名
        author_id: '', // 存放用户uid
        userInfo: {}, //个人信息存放
        articleList: {}, //所有用户发布的文章详情信息
        searchList: [], // 搜索关键字返回的数据信息
        replyList: [], // 存放评论的列表
        replyListTwo: {}, // 存放二级回复的用户信息
        Replytolist: [], //存放二级回复的列表信息
    },
    getters: {},
    mutations: {
        //获取分类标题和分类的ID
        getTitle(state, payload) {
            if (state.titles.length > 7) return
            state.titles.push(payload.name)
            state.id.push(payload.id)
        },
        // 获取个人资料页面传过来的个人昵称
        getAuthor(state, name) {
            state.author = name
            console.log(name);
        },
        // 获取 用户ID 
        getAuthorId(state, uid) {
            state.author_id = uid
                // state.loginresult = result
        },
        //获取最新的userinfo
        getUserInfo(state, userinfo) {
            state.userInfo = userinfo
        },
        //获取用户发布文章详情页的数据
        getArticleList(state, articleList) {
            state.articleList = articleList
        },
        //获取搜索返回的列表信息
        getSearchList(state, searchList) {
            state.searchList = searchList
        },
        // 获取评论的列表
        getReplyList(state, replyList) {
            state.replyList.push(...replyList)
        },
        getReplyList2(state, replyList) {
            state.replyList.unshift(replyList)
        },
        getReplyList3(state, replyList) {
            state.replyList.shift()
        },
        // 点击时 获取到被回复者的参数
        replyListTwo(state, replyList) {
            state.replyListTwo = replyList
        },
        // 点击列表获取到回复的评论信息
        getreplylist(state, content) {
            state.Replytolist = content
        },
        getreplylist2(state, content) {
            state.Replytolist.unshift(content)
        },
    },
    actions: {
        async getTitleList(context) {
            const res = await axios.post('https://d33a5037-6c63-4e92-8bb1-30018ab701ea.bspapp.com/http/api/get_cate_list')
            const result = res.data.data
            result.forEach(item => {
                let a = {
                    name: item.name,
                    id: item._id
                }
                context.commit('getTitle', a)
            });

        },
        // 评论列表和二级评论
        getReplyAll(context, payload) {
            let reply_comment_id = sessionStorage.getItem('reply_comment_id')
            let { comment_type, content } = payload
            let userInfo = JSON.parse(localStorage.getItem('userInfo'))
            let { uid: user_id } = localStorage;
            let article_id = JSON.parse(localStorage.getItem("articleList2"))._id;
            console.log(article_id, comment_type, content, user_id);
            addreply(user_id, article_id, comment_type, content, reply_comment_id).then((res) => {
                if (res.data.code == 0) {
                    if (comment_type == 0) {
                        let comentData = {
                            article_id,
                            comment_type,
                            content,
                            create_time: new Date().getTime(),
                            info: {
                                avatar: userInfo.avatar,
                                nickname: userInfo.nickname,
                                username: userInfo.username,
                                _id: userInfo._id,
                            },
                            is_like: false,
                            like_count: 0,
                            reply_comment_id: 0,
                            reply_num: 0,
                            user_id,
                            _id: res.data.data.id,
                        };
                        context.commit('getReplyList2', comentData)
                        context.commit('getreplylist2', comentData)
                    }
                }
                // console.log(res);
            });
        }
    },
    modules: {

    }
})